<?php
/**
 * Created by PhpStorm.
 * User: Xwen
 * Date: 2018/3/21
 * Time: 20:19
 *
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘
 *             │ ─┤ ─┤       │ ─┤ ─┤
 *             └──┴──┘       └──┴──┘
 *                 神兽保佑
 *                 代码无BUG!
 *"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”- Seymour Cray
 */
class RoleModel extends Model{
    public $table = "authority_role";
    public $error = "请重新尝试……";
    public function __construct($table = NULL)
    {
        $table = $this->table;
        parent::__construct($table);
    }
    public function get_all(){
        return $this->where("status=1")->all();
    }
    public function get_one_page(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        return $this->where("status=1")->order('updated_time asc')->pagination($page,10);
    }
    public function get_one(){
        return $this->where("status=1 and id=".$_GET['id'])->find();
    }
    public function update_data(){
        if(!$this->validate()){
            return false;
        }
        $_POST['updated_time'] = time();
        return $this->where("id=".$_GET['id'])->update();
    }
    public function add_data(){
        if(!$this->validate()){
            return false;
        }
        $_POST['created_time'] = time();
        $_POST['updated_time'] = time();
        return $this->add();
    }
    public function validate(){
        if(empty($_POST['name'])){
            $this->error = "角色名不能为空";
            return false;
        }
        return true;
    }
}